package com.mzpai.web.controller;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.imageio.ImageIO;
import javax.imageio.stream.ImageOutputStream;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.gexin.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.google.common.base.Joiner;
import com.mzpai.product.constant.ProductConstants;
import com.mzpai.product.exception.ProductResException;
import com.mzpai.product.exception.ProductResExceptionCode;
import com.mzpai.product.model.StoreBrand;
import com.mzpai.product.model.StoreCategory;
import com.mzpai.product.model.StoreProduct;
import com.mzpai.product.model.StoreProductAttrValue;
import com.mzpai.product.model.StoreSeckill;
import com.mzpai.product.model.SystemConfig;
import com.mzpai.product.service.StoreBrandService;
import com.mzpai.product.service.StoreCategoryService;
import com.mzpai.product.service.StoreProductAttrService;
import com.mzpai.product.service.StoreProductAttrValueService;
import com.mzpai.product.service.StoreProductCateService;
import com.mzpai.product.service.StoreProductDetailsService;
import com.mzpai.product.service.StoreProductService;
import com.mzpai.product.service.StoreProductSupplierService;
import com.mzpai.product.service.StoreSeckillService;
import com.mzpai.product.service.StoreUserSeckillCollectService;
import com.mzpai.product.service.SystemConfigService;
import com.mzpai.product.service.SystemStoreService;
import com.mzpai.product.util.QRCodeUtil;
import com.mzpai.product.vo.ShareStoreProductInfoVo;
import com.mzpai.product.vo.StoreProductDeleteVo;
import com.mzpai.product.vo.StoreProductDto;
import com.mzpai.product.vo.StoreProductIdAndCateIdVo;
import com.mzpai.product.vo.StoreProductPutDownReqVo;
import com.mzpai.product.vo.StoreProductQueryReqVo;
import com.mzpai.product.vo.StoreProductSkuDto;
import com.mzpai.product.vo.StoreProductSkuQueryReqVo;
import com.mzpai.product.vo.UpdateStoreProductWaitPutReqVo;
import com.mzpai.util.BeanUtils;
import com.mzpai.util.DateUtil;
import com.mzpai.util.StringUtils;
import com.mzpai.vo.ResponseVo;
import com.mzpai.web.utils.StorageUploadUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
/**
 *
 * <b>供应商商品表 | Controller</b><br>
 *
 * guguanfei 2021-03-16 14:43:32
 * 
 *  @version 1.0.0
 *
 */
@Api(value = "店铺商品数据接口")
@RestController
@RequestMapping("/storeProduct")
public class StoreProductController extends BaseController {
	
	private static Logger log = LoggerFactory.getLogger(StoreProductController.class);

	@Autowired 
	private StoreProductService storeProductService;
	@Autowired 
	private SystemConfigService systemConfigService;
	@Autowired
	private StoreCategoryService storeCategoryService;
	@Value("${mzpai.share.productUrl}")
	private String shareProductUrl;
	
	@Autowired
	private StoreProductCateService storeProductCateService;
	
	@Autowired
	private StoreProductDetailsService storeProductDetailsService;
	
	@Autowired
	private StoreProductAttrValueService storeProductAttrValueService;
	
	
	@Autowired
	private StoreProductAttrService storeProductAttrService;
	
	@Autowired 
	private StoreProductSupplierService storeProductSupplierService;
	
	@Autowired
	private StoreBrandService storeBrandService;
	
	@Autowired 
	private SystemStoreService systemStoreService;
	
	@Autowired 
	private StoreSeckillService storeSeckillService;
	
	@Autowired
	private StoreUserSeckillCollectService storeUserSeckillCollectService;
	
	/**
	 * 
	 * @param queryProductByPageStr
	 * @return
	 */
	@ApiOperation(value = "根据条件分页查询店铺下商品数据")
    @RequestMapping(value = "/queryProductByPage",method = {RequestMethod. POST ,RequestMethod. GET })
    public ResponseVo<PageInfo<StoreProductDto>> queryProductByPage(
    		@ApiParam(required = true, name = "queryProductByPageStr", value = "入参为queryProductByPageStr")
    		@RequestParam(value = "queryProductByPageStr", required = true) String queryProductByPageStr){
		
		log.info("msg1=开始根据条件分页查询店铺下商品数据,,method=queryProductByPage,,reqVo={}",queryProductByPageStr);
		StoreProductQueryReqVo storeProductReqVo = JSONObject.parseObject(queryProductByPageStr, StoreProductQueryReqVo.class);
		String cateIdStr=storeProductReqVo.getCateId();
		Integer stroeId=storeProductReqVo.getStoreId();
		Integer pageIndex=storeProductReqVo.getPageIndex();
		Integer pageSize=storeProductReqVo.getPageSize();
		Integer isShelves=storeProductReqVo.getIsShelves();
		String storeNameOrBarCode=storeProductReqVo.getStoreNameOrBarCode();
		if(stroeId==null){
			ResponseVo<PageInfo<StoreProductDto>> resVo=new ResponseVo<PageInfo<StoreProductDto>>();
			resVo.setCode(ProductResExceptionCode.STOREID_NULL.getCode());
			resVo.setMessage(ProductResExceptionCode.STOREID_NULL.getMessage());
			return resVo;
		}
		
        List<String> cateIdList=null;
		if(!StringUtils.isNullOrBlank(cateIdStr)){
			cateIdList=new ArrayList<String>();
			String cateIdArray[]=cateIdStr.split(",");
			for(String cateStr:cateIdArray){
				cateIdList.add(cateStr);
			}
		}
		
		
		//根据顶层父类id查询所有叶子节点
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("storeId", stroeId);
		map.put("isShow", isShelves);
		
		if(cateIdList!=null && !cateIdList.isEmpty()){
			map.put("cateIdList", cateIdList);
		}
		map.put("storeName", storeNameOrBarCode);
		map.put("type", 0);
		map.put("isDel",0);
		PageInfo<StoreProductDto> storeProductPageInfo=storeProductService.getProcuctAndBrandListByPage(map, pageIndex, pageSize);
		//copy对象
		List<StoreProductDto> listStoreProductDto= storeProductPageInfo.getList();
		if(listStoreProductDto!=null && !listStoreProductDto.isEmpty()){
			for(StoreProductDto storeProductDto:listStoreProductDto){
				storeProductDto.setProductUrl(shareProductUrl+"&id="+storeProductDto.getId());
			}
		}
		ResponseVo<PageInfo<StoreProductDto>> resVo=new ResponseVo<PageInfo<StoreProductDto>>();
		resVo.setResult(storeProductPageInfo);
		log.info("msg1=结束根据条件分页查询店铺下商品数据,,method=queryProductByPage");
    	return resVo;
    }
	
	
	
	@ApiOperation(value = "根据商品编码查询店铺商品详情")
    @RequestMapping(value = "/queryProductById", method = RequestMethod.POST)
    public ResponseVo<UpdateStoreProductWaitPutReqVo> queryProductById(
    		@ApiParam(required = true, name = "id", value = "入参为商品id")
    		@RequestParam(value = "id", required = true) Integer id){
		
		log.info("msg1=根据商品主键查询店铺商品详情,,method=queryProductByProductCode,,id={}",id);
		//基本信息
		StoreProduct storeProduct=storeProductService.queryViewById(id);
		UpdateStoreProductWaitPutReqVo vo = new UpdateStoreProductWaitPutReqVo();
		BeanUtils.copyProperties(storeProduct, vo);
		
		Integer brandId=vo.getBrandId();
		StoreBrand storeBrand=storeBrandService.queryByPrimaryId(brandId);
		if(storeBrand!=null){
			vo.setBrandName(storeBrand.getBrandName());
		}
		Integer storeId=storeProduct.getStoreId();
		/**商品名称*/
		addCateName(vo,storeId);
		
		//商品详情
		vo.setStoreProductDetailsList(storeProductDetailsService.getListByProductId(id));
		//商品商品属性,单规格,多规格
		vo.setStoreProductAttrList(storeProductAttrService.getListByProductId(id));
		//商品属性值表,前端根据商品属性组合多条
		vo.setStoreProductAttrValueList(storeProductAttrValueService.getListByProductId(id));
		ResponseVo<UpdateStoreProductWaitPutReqVo> resVo=new ResponseVo<UpdateStoreProductWaitPutReqVo>();
		resVo.setResult(vo);
		return resVo;
    }
	
	/**
	 * 添加名称
	 * @param vo
	 */
	private void addCateName(UpdateStoreProductWaitPutReqVo vo,Integer storeId) {
		if(StringUtils.isNoneBlank(vo.getCateId())) {
			String cateName = "";
			for(String s : vo.getCateId().split(",")) {
				StoreCategory storeCategory=storeCategoryService.queryById(s);
				if(storeCategory!=null){
					Integer cateStoreId=storeCategory.getStoreId();
					//只有分类是门店自己的才设置值 
					if(cateStoreId!=null && storeId!=null && cateStoreId.intValue()==storeId.intValue()){
						cateName = storeCategory.getCateName() + ",";
					}
				}
			}
			if(!StringUtils.isNullOrBlank(cateName)){
				vo.setCateName(StringUtils.removeLastChar(cateName));
			}else{
				vo.setCateId(null);
			}
		}
	}



	@ApiOperation(value = "根据商品id查询店铺商品详情")
    @RequestMapping(value = "/getViewProductById")
    public ResponseVo<UpdateStoreProductWaitPutReqVo> getViewProductById(@RequestParam(value = "id", required = true) Integer id){
		log.info("msg1=根据商品主键查询店铺商品详情,,method=queryProductByProductCode,,id={}",id);
		//基本信息
		StoreProduct storeProduct=storeProductService.queryByPrimaryId(id);
		UpdateStoreProductWaitPutReqVo vo = new UpdateStoreProductWaitPutReqVo();
		BeanUtils.copyProperties(storeProduct, vo);		
		Integer brandId=vo.getBrandId();
		StoreBrand storeBrand=storeBrandService.queryByPrimaryId(brandId);
		if(storeBrand!=null){
			vo.setBrandName(storeBrand.getBrandName());
		}
		Integer storeId=storeProduct.getStoreId();
		/**商品名称*/
		addCateName(vo,storeId);
		//商品详情
		vo.setStoreProductDetailsList(storeProductDetailsService.getListByProductId(id));
		//商品商品属性,单规格,多规格
		vo.setStoreProductAttrList(storeProductAttrService.getListByProductId(id));
		//商品属性值表,前端根据商品属性组合多条
		vo.setStoreProductAttrValueList(storeProductAttrValueService.getListByProductId(id));
		ResponseVo<UpdateStoreProductWaitPutReqVo> resVo=new ResponseVo<UpdateStoreProductWaitPutReqVo>();
		resVo.setResult(vo);
		return resVo;
	}
	
	

	@ApiOperation(value = "门店新增商品")
    @RequestMapping(value = "/addProduct")
    public ResponseVo<UpdateStoreProductWaitPutReqVo> addProduct(@RequestParam(value = "product", required = true) String product){
		log.info("msg1=门店新增商品,,method=addProduct,,product={}", product);
		ResponseVo<UpdateStoreProductWaitPutReqVo> resVo=new ResponseVo<UpdateStoreProductWaitPutReqVo>();
		try{
			UpdateStoreProductWaitPutReqVo vo = JSONObject.parseObject(product, UpdateStoreProductWaitPutReqVo.class);
			
			Integer storeId=vo.getStoreId();
			
			if(storeId==null){
				ResponseVo<UpdateStoreProductWaitPutReqVo> resultVo=new ResponseVo<UpdateStoreProductWaitPutReqVo>();
				resultVo.setCode(ProductResExceptionCode.STOREID_NULL.getCode());
				resultVo.setMessage(ProductResExceptionCode.STOREID_NULL.getMessage());
				return resultVo;
			}
			
			if(StringUtils.isBlank(vo.getStoreName()) || StringUtils.isBlank(vo.getCateId() + "")
					|| StringUtils.isBlank(vo.getImage())) {
				
				ResponseVo<UpdateStoreProductWaitPutReqVo> resultVo=new ResponseVo<UpdateStoreProductWaitPutReqVo>();
				resultVo.setCode(ProductResExceptionCode.ID_NULL.getCode());
				resultVo.setMessage("商品名称，分类，图片,品牌名称 不能为空!");
				return resultVo;
			}
			
			
			/**检查商品名称是否包含禁用关键字*/
			String keyWords = checkKeyWord(vo.getStoreName());
			if(StringUtils.isNoneBlank(keyWords)) {
				resVo.setCode(-1);
				resVo.setMessage("商品名称存在关键字[" + StringUtils.unicodeToString(keyWords) + "]");
				return resVo;
			}
			
			/**
			 * 实际物品类商品进行条形码校验:门店下条形码存在则不进行下一步操作
			 * 商品服务类型 1:实物类; 2:服务类
			 */
		    String productType = vo.getProductType();
			if(StringUtils.isNullOrBlank(productType)){
				productType=ProductConstants.PRODUCT_TYPE_1;
				vo.setProductType(ProductConstants.PRODUCT_TYPE_1);
			}
		    
			if(productType.equals(ProductConstants.PRODUCT_TYPE_1)){
				List<StoreProductAttrValue> attrValueList = vo.getStoreProductAttrValueList();
				for(StoreProductAttrValue attrValueVo:attrValueList){
					String attrValueBarCode=attrValueVo.getBarCode();
					String attrValueSuk=attrValueVo.getSuk();
					
					if(StringUtils.isNullOrBlank(attrValueBarCode)){
						/*ResponseVo<UpdateStoreProductWaitPutReqVo> resultVo=new ResponseVo<UpdateStoreProductWaitPutReqVo>();
						resultVo.setCode(ProductResExceptionCode.BARCODE_NULL.getCode());
						resultVo.setMessage("规格["+attrValueSuk+"]"+"对应的条形码为空!!!");
						return  resultVo;*/
						//设置默认条形码
						attrValueVo.setBarCode("111111"+DateUtil.getDateFormateDate()+StringUtils.randomString(6,true));
					}
					
					Map<String,Object> attrValueMap=new HashMap<String,Object>();
					attrValueMap.put("storeId", storeId);
					attrValueMap.put("barCode", attrValueVo.getBarCode());
					attrValueMap.put("state", 1);
					List<StoreProductAttrValue> queryAttrValueList=storeProductAttrValueService.getList(attrValueMap);
					
					if(queryAttrValueList!=null && !queryAttrValueList.isEmpty()){
						ResponseVo<UpdateStoreProductWaitPutReqVo> resultVo=new ResponseVo<UpdateStoreProductWaitPutReqVo>();
						resultVo.setCode(ProductResExceptionCode.BARCODE_EXTEND_ERROR.getCode());
						resultVo.setMessage("条形码["+attrValueBarCode+"]"+"已存在");
						return  resultVo;
					}
				}
			}
			
			int id = storeProductService.saveInfo(vo);
			vo.setId(id);
			resVo.setResult(vo);
		} catch(Exception e) {
			logger.error("msg1= 保存门店商品信息异常， {}", e);
			resVo.setCode(-1);
			resVo.setMessage("操作失败!");
		}
    	return resVo;
	}
	
	/**
	 * 查询商品是否关键字， 获取配置
	 * @param storeName
	 * @return
	 */
	private String checkKeyWord(String storeName) {
		String str = null;
		SystemConfig systemConfig = systemConfigService.getConfigByKey("disabled_keys");
		if(systemConfig != null) {
			String smp = systemConfig.getValue().replace("\"", "");
			for(String key : smp.split(",")) {
				if(storeName.indexOf(StringUtils.unicodeToString(key)) >= 0) {
					str = key;
					break;
				}
			}
		}
		return str;
	}



	@ApiOperation(value = "店铺商品编辑提交")
    @RequestMapping(value = "/updateProduct")
    public ResponseVo<Boolean> updateProduct(@ApiParam(required = true, name = "product", value = "入参为product")
    		@RequestParam(value = "product", required = true) String product){
		log.info("msg1=店铺商品编辑提交,,method=updateProduct,,product={}",product);
		UpdateStoreProductWaitPutReqVo updateStoreProductWaitPutReqVo = JSONObject.parseObject(product, UpdateStoreProductWaitPutReqVo.class);
		ResponseVo<Boolean> resVo=new ResponseVo<Boolean>();
		Integer id = updateStoreProductWaitPutReqVo.getId();
		if(id==null){
			resVo.setCode(ProductResExceptionCode.ID_NULL.getCode());
			resVo.setMessage(ProductResExceptionCode.ID_NULL.getMessage());
			return resVo;
		}
		
		Integer storeId=updateStoreProductWaitPutReqVo.getStoreId();
		
		//商品服务类型 1:实物类; 2:服务类   兼容老版本 先不做必填  没有就默认实物
		String productType=updateStoreProductWaitPutReqVo.getProductType();
		if(StringUtils.isNullOrBlank(productType)){
			productType=ProductConstants.PRODUCT_TYPE_1;
			updateStoreProductWaitPutReqVo.setProductType(ProductConstants.PRODUCT_TYPE_1);
//			resVo.setCode(ProductResExceptionCode.PRODUCT_TYPE_NULL.getCode());
//			resVo.setMessage(ProductResExceptionCode.PRODUCT_TYPE_NULL.getMessage());
//			return resVo;
		}
		
		String cateId=updateStoreProductWaitPutReqVo.getCateId();
		if(StringUtils.isBlank(cateId)) {
			resVo.setCode(ProductResExceptionCode.CATEID_NULL.getCode());
			resVo.setMessage("商品分类CATEID为空");
			return resVo;
		}
		
		/**检查商品名称是否包含禁用关键字*/
		String keyWords = checkKeyWord(updateStoreProductWaitPutReqVo.getStoreName());
		if(StringUtils.isNoneBlank(keyWords)) {
			resVo.setCode(-1);
			resVo.setMessage("商品名称存在关键字[" + keyWords + "]");
			return resVo;
		}
		//校验条形码是否存在  待处理 前端传主键判断
		if(productType.equals(ProductConstants.PRODUCT_TYPE_1)){
			List<StoreProductAttrValue> attrValueList = updateStoreProductWaitPutReqVo.getStoreProductAttrValueList();
			for(StoreProductAttrValue attrValueVo:attrValueList){
				String attrValueBarCode=attrValueVo.getBarCode();
				String attrValueSuk=attrValueVo.getSuk();
				if(StringUtils.isNullOrBlank(attrValueBarCode)){
					resVo.setCode(ProductResExceptionCode.BARCODE_NULL.getCode());
					resVo.setMessage(ProductResExceptionCode.BARCODE_NULL.getMessage());
					return  resVo;
				}
				
				Map<String,Object> attrValueMap=new HashMap<String,Object>();
				attrValueMap.put("storeId", storeId);
				attrValueMap.put("barCode", attrValueBarCode);
				attrValueMap.put("state", 1);
				List<StoreProductAttrValue> queryAttrValueList=storeProductAttrValueService.getList(attrValueMap);
				
				if(queryAttrValueList!=null && !queryAttrValueList.isEmpty() && queryAttrValueList.size()>1){
					resVo.setCode(ProductResExceptionCode.BARCODE_EXTEND_ERROR.getCode());
					resVo.setMessage("条形码["+attrValueBarCode+"]"+"已存在");
					return  resVo;
				}
				/*if(queryAttrValueList!=null && !queryAttrValueList.isEmpty()){
					resVo.setCode(ProductResExceptionCode.BARCODE_EXTEND_ERROR.getCode());
					resVo.setMessage("条形码["+attrValueBarCode+"]"+"已存在");
					return  resVo;
				}*/
			}
		}
			
	    StoreProduct currentStoreProduct=storeProductService.queryByPrimaryId(id);
		if(currentStoreProduct!=null){
			storeProductService.updateInfo(updateStoreProductWaitPutReqVo);
		}else{
			resVo.setCode(ProductResExceptionCode.PRODUCT_NOT_EXTEND_ERROR.getCode());
			resVo.setMessage(ProductResExceptionCode.PRODUCT_NOT_EXTEND_ERROR.getMessage());
			resVo.setResult(false);
			return resVo;
		}
		resVo.setCode(ProductResExceptionCode.SUCCESS.getCode());
		resVo.setMessage(ProductResExceptionCode.SUCCESS.getMessage());
		resVo.setResult(true);
		log.info("msg1=店铺商品编辑提交,,method=updateStoreProductWaitPutByProductCode");
    	return resVo;
    }
		
    /**
     * 
     * @param storeProductPutDownReqVo
     * @return
     */
	@ApiOperation(value = "店铺商品上架/下架")
    @RequestMapping(value = "/storeProductPutDown", method = RequestMethod.POST)
    public ResponseVo<Boolean> storeProductPutDown(
    		@ApiParam(required = true, name = "storeProductPutDownStr", value = "入参为storeProductPutDownStr")
    		@RequestParam(value = "storeProductPutDownStr", required = true) String storeProductPutDownStr){
		
		StoreProductPutDownReqVo storeProductPutDownReqVo = JSONObject.parseObject(storeProductPutDownStr, StoreProductPutDownReqVo.class);
		
		log.info("msg1=根据商品主键查询店铺商品详情,,method=storeProductPutDown,,reqVo={}",JSON.toJSONString(storeProductPutDownReqVo));
		Integer storeId=storeProductPutDownReqVo.getStoreId();
		Integer isShow=storeProductPutDownReqVo.getIsShow();
		List<StoreProductIdAndCateIdVo> storeProductIdAndCateIdVoList=storeProductPutDownReqVo.getStoreProductIdAndCateIdVoList();
		
		
		if(storeId==null){
			ResponseVo<Boolean> resultVo=new ResponseVo<Boolean>();
			resultVo.setCode(ProductResExceptionCode.STOREID_NULL.getCode());
			resultVo.setMessage(ProductResExceptionCode.STOREID_NULL.getMessage());
			return resultVo;
//			throw new ProductResException(ProductResExceptionCode.STOREID_NULL.getCode(),ProductResExceptionCode.STOREID_NULL.getMessage());
		}
		
		if(isShow==null){
			ResponseVo<Boolean> resultVo=new ResponseVo<Boolean>();
			resultVo.setCode(ProductResExceptionCode.ISSHOW_NULL.getCode());
			resultVo.setMessage(ProductResExceptionCode.ISSHOW_NULL.getMessage());
			return resultVo;
//			throw new ProductResException(ProductResExceptionCode.ISSHOW_NULL.getCode(),ProductResExceptionCode.ISSHOW_NULL.getMessage());
		}
		
		
		if(storeProductIdAndCateIdVoList==null || storeProductIdAndCateIdVoList.isEmpty()){
			ResponseVo<Boolean> resultVo=new ResponseVo<Boolean>();
			resultVo.setCode(ProductResExceptionCode.PRODUCTCODE_LIST_NULL.getCode());
			resultVo.setMessage(ProductResExceptionCode.PRODUCTCODE_LIST_NULL.getMessage());
			return resultVo;
//			throw new ProductResException(ProductResExceptionCode.PRODUCTCODE_LIST_NULL.getCode(),ProductResExceptionCode.PRODUCTCODE_LIST_NULL.getMessage());
		}
		
		try{
			for(StoreProductIdAndCateIdVo storeProductIdAndCateIdVo:storeProductIdAndCateIdVoList){
				
				Integer id=storeProductIdAndCateIdVo.getId();
				String cateId=storeProductIdAndCateIdVo.getCateId();
				BigDecimal price=storeProductIdAndCateIdVo.getPrice();
				StoreProduct storeProduct=storeProductService.queryByPrimaryId(id);
				if(storeProduct==null ){
					ResponseVo<Boolean> resVo=new ResponseVo<Boolean>();
					resVo.setCode(ProductResExceptionCode.PRODUCT_NOT_EXTEND_ERROR.getCode());
					resVo.setMessage("门店号["+storeId+"] ["+id+"] "+ProductResExceptionCode.PRODUCT_NOT_EXTEND_ERROR.getMessage());
					return resVo;
				}
				StoreProduct updateStoreProduct=new StoreProduct();
				//规格 0单 1多
				Integer specType=storeProduct.getSpecType();
				Integer productStock=storeProduct.getStock();
				
				//下架
				if(isShow.intValue()==ProductConstants.IS_SHOW0){
					updateStoreProduct.setId(storeProduct.getId());
					updateStoreProduct.setIsShow(isShow);
					storeProductService.update(updateStoreProduct);
				}else{
					//单规格上架
					if(specType.intValue()==ProductConstants.SINGLE_SPEC_TYPE.intValue()){
						if(isShow.intValue()==ProductConstants.IS_SHOW1 && price==null){
							throw new ProductResException(ProductResExceptionCode.PRICE_NULL.getCode(),ProductResExceptionCode.PRICE_NULL.getMessage());
						}
						List<StoreProductAttrValue> listStoreAttrValue=storeProductAttrValueService.getListByProductId(id);
						if(listStoreAttrValue!=null && !listStoreAttrValue.isEmpty()){
							
							StoreProductAttrValue queryStoreProductAttrValue=listStoreAttrValue.get(0);
							
							//修改规格立的价格
							StoreProductAttrValue storeProductAttrValue=new StoreProductAttrValue();
							
							Integer productAttrValueStock=queryStoreProductAttrValue.getStock();
							if(productAttrValueStock==null || productAttrValueStock.intValue()==0){
								storeProductAttrValue.setStock(99);
							}
							if(price!=null){
								updateStoreProduct.setPrice(price);
								updateStoreProduct.setMinPrice(price);
								storeProductAttrValue.setMinPrice(price);
								storeProductAttrValue.setPrice(price);
							}
							storeProductAttrValue.setProductId(id);
							storeProductAttrValue.setType(ProductConstants.ACTIVE_TYPE_0);
						    storeProductAttrValueService.updateProductIdAndType(storeProductAttrValue);
						}
							
						if(productStock==null || productStock.intValue()==0){
							updateStoreProduct.setStock(99);
						}
						updateStoreProduct.setId(storeProduct.getId());
						updateStoreProduct.setCateId(cateId);
						updateStoreProduct.setIsShow(isShow);
						storeProductService.update(updateStoreProduct);
				}
					
				}
			}
		}catch(Exception e){
			ResponseVo<Boolean> resultVo=new ResponseVo<Boolean>();
			resultVo.setCode(ProductResExceptionCode.SAVE_ERROR.getCode());
			resultVo.setMessage(ProductResExceptionCode.SAVE_ERROR.getMessage());
			return resultVo;
//			throw new ProductResException(ProductResExceptionCode.SAVE_ERROR.getCode(),ProductResExceptionCode.SAVE_ERROR.getMessage());
		}
		
		ResponseVo<Boolean> resVo=new ResponseVo<Boolean>();
		resVo.setCode(ProductResExceptionCode.SUCCESS.getCode());
		resVo.setMessage(ProductResExceptionCode.SUCCESS.getMessage());
		resVo.setResult(true);
		log.info("msg1=根据主键查询店铺商品详情结束,,method=storeProductPutDown");
    	return resVo;
    }
	
	
	/**
     * 
     * @param storeProductPutDownReqVo
     * @return
     */
	@ApiOperation(value = "店铺商品批量删除")
    @RequestMapping(value = "/storeProductDelete", method = RequestMethod.POST)
    public ResponseVo<Boolean> storeProductDelete(
    		@ApiParam(required = true, name = "storeProductDelStr", value = "入参为storeProductDelStr")
    		@RequestParam(value = "storeProductDelStr", required = true) String storeProductDelStr){
		
		StoreProductDeleteVo storeProductDeleteVo = JSONObject.parseObject(storeProductDelStr, StoreProductDeleteVo.class);
		log.info("msg1=根据商品主键店铺商品批量删除开始,,method=storeProductDelete,,reqVo={}",JSON.toJSONString(storeProductDeleteVo));
		
		Integer storeId=storeProductDeleteVo.getStoreId();
		List<Integer> idList=storeProductDeleteVo.getIdList();
		
		if(storeId==null){
			ResponseVo<Boolean> resultVo=new ResponseVo<Boolean>();
			resultVo.setCode(ProductResExceptionCode.STOREID_NULL.getCode());
			resultVo.setMessage(ProductResExceptionCode.STOREID_NULL.getMessage());
			return resultVo;
//			throw new ProductResException(ProductResExceptionCode.STOREID_NULL.getCode(),ProductResExceptionCode.STOREID_NULL.getMessage());
		}
		
		if(idList==null || idList.isEmpty()){
			ResponseVo<Boolean> resultVo=new ResponseVo<Boolean>();
			resultVo.setCode(ProductResExceptionCode.PRODUCTCODE_LIST_NULL.getCode());
			resultVo.setMessage(ProductResExceptionCode.PRODUCTCODE_LIST_NULL.getMessage());
			return resultVo;
//			throw new ProductResException(ProductResExceptionCode.PRODUCTCODE_LIST_NULL.getCode(),ProductResExceptionCode.PRODUCTCODE_LIST_NULL.getMessage());
		}
		
		try{
			for(Integer productId:idList){
				//此次不做真正删除,只修改is_del状态为1
//				storeProductService.delStoreProductAndDetailTable(productId, storeId);
				StoreProduct storeProduct=storeProductService.queryByPrimaryId(productId);
				if(storeProduct!=null){
					StoreProduct upStoreProduct=new StoreProduct();
					upStoreProduct.setId(productId);
					upStoreProduct.setIsDel(1);
					storeProductService.update(upStoreProduct);
					//规格也更新无效
					StoreProductAttrValue upStoreProductAttrValue=new StoreProductAttrValue();
					upStoreProductAttrValue.setProductId(productId);
					upStoreProductAttrValue.setState(0);
					storeProductAttrValueService.updateByProductId(upStoreProductAttrValue);
				}
			}
		}catch(Exception e){
			e.printStackTrace();
			ResponseVo<Boolean> resultVo=new ResponseVo<Boolean>();
			resultVo.setCode(ProductResExceptionCode.DEL_ERROR.getCode());
			resultVo.setMessage(ProductResExceptionCode.DEL_ERROR.getMessage());
			return resultVo;
//			throw new ProductResException(ProductResExceptionCode.DEL_ERROR.getCode(),ProductResExceptionCode.DEL_ERROR.getMessage());
		}
		
		ResponseVo<Boolean> resVo=new ResponseVo<Boolean>();
		resVo.setCode(ProductResExceptionCode.SUCCESS.getCode());
		resVo.setMessage(ProductResExceptionCode.SUCCESS.getMessage());
		log.info("msg1=根据商品主键店铺商品批量删除结束,,method=storeProductDelete");
    	return resVo;
    }	
	
	

	@ApiOperation(value = "商品管理总数统计")
    @RequestMapping(value = "/productCount")
    public ResponseVo<Map<String,Object>> productCount(@RequestParam(value = "storeId", required = true) String storeId){
		log.info("msg1=商品管理总数统计开始,,method=productCount,,storeId={}", storeId);
		
//		ProductCountVo vo = JSONObject.parseObject(countStr, ProductCountVo.class);
		
		ResponseVo<Map<String,Object>> resVo=new ResponseVo<Map<String,Object>>();
		
		Map<String,Object> countMap=new HashMap<String,Object>();
		
		if(storeId==null) {
			ResponseVo<Map<String,Object>> resultVo=new ResponseVo<Map<String,Object>>();
			resultVo.setCode(ProductResExceptionCode.STOREID_NULL.getCode());
			resultVo.setMessage(ProductResExceptionCode.STOREID_NULL.getMessage());
			return resultVo;
//			throw new ProductResException(ProductResExceptionCode.STOREID_NULL.getCode(),"门店id不能为空!");
		}
		//未上架
		Map<String,Object> storeProductMap0=new HashMap<String,Object>();
		storeProductMap0.put("storeId", storeId);
		storeProductMap0.put("isShow", ProductConstants.IS_SHOW0);
		storeProductMap0.put("isDel", 0);
		Integer storeCount0=storeProductService.countTotal(storeProductMap0);
		//已上架
		Map<String,Object> storeProductMap1=new HashMap<String,Object>();
		storeProductMap1.put("storeId", storeId);
		storeProductMap1.put("isShow", ProductConstants.IS_SHOW1);
		storeProductMap0.put("isDel", 0);
		Integer storeCount1=storeProductService.countTotal(storeProductMap1);
		
		
		Map<String,Object> storeProductSupplierMap=new HashMap<String,Object>();
//		storeProductSupplierMap.put("factoryId", ProductConstants.FACTORY_ID);
		
		Integer storeProductSupplierCount=storeProductSupplierService.countTotal(storeProductSupplierMap);
			
		countMap.put("noshelves", storeCount0);
		countMap.put("isshelves", storeCount1);
		countMap.put("goodlibrary", storeProductSupplierCount);
		resVo.setCode(ProductResExceptionCode.SUCCESS.getCode());
		resVo.setMessage(ProductResExceptionCode.SUCCESS.getMessage());
		resVo.setResult(countMap);
    	return resVo;
	}
	
	
	
	@ApiOperation(value = "根据条形码校验是否存在,存在返回true,不存在返回false")
    @RequestMapping(value = "/verifyStoreBarCode",method = {RequestMethod. POST ,RequestMethod. GET })
    public ResponseVo<Boolean> verifyStoreBarCode(
    		@ApiParam(required = true, name = "operate", value = "操作标识,新增时校验operate=add,修改operate=update")
    		@RequestParam(value = "operate", required = true) String operate,
    		@ApiParam(required = true, name = "id", value = "商品id")
    		@RequestParam(value = "id", required = false) Integer id,
    		@ApiParam(required = true, name = "storeId", value = "门店id")
    		@RequestParam(value = "storeId", required = true) Integer storeId,
    		@ApiParam(required = true, name = "barCode", value = "条形码")
    		@RequestParam(value = "barCode", required = true) String barCode){
		
		log.info("msg1=根据条形码校验是否存在,存在返回true开始,,method=verifyStoreBarCode,,id={},,storeId={},,barCode={}", id,storeId,barCode);
		
    	ResponseVo<Boolean> resVo=new ResponseVo<Boolean>();
    	boolean result=false;
    	Map<String,Object> queryMap=new HashMap<String,Object>();
    	queryMap.put("productId", id);
    	queryMap.put("storeId", storeId);
    	queryMap.put("barCode", barCode);
    	
    	if(StringUtils.isNullOrBlank(operate)){
    		resVo.setCode(ProductResExceptionCode.BARCODE_NULL.getCode());
    		resVo.setMessage(ProductResExceptionCode.BARCODE_NULL.getMessage());
    		resVo.setResult(true);
    		return resVo;
    	}
        if(operate.equals(ProductConstants.OPERATE_UPDATE)){
        	if(id==null){
        		resVo.setCode(ProductResExceptionCode.ID_NULL.getCode());
        		resVo.setMessage(ProductResExceptionCode.ID_NULL.getMessage());
        		resVo.setResult(true);
        		return resVo;
        	}
    	}
    	
    	if(storeId==null){
    		resVo.setCode(ProductResExceptionCode.STOREID_NULL.getCode());
    		resVo.setMessage(ProductResExceptionCode.STOREID_NULL.getMessage());
    		resVo.setResult(true);
    		return resVo;
    	}
    	
    	if(StringUtils.isNullOrBlank(barCode)){
    		resVo.setCode(ProductResExceptionCode.BARCODE_NULL.getCode());
    		resVo.setMessage(ProductResExceptionCode.BARCODE_NULL.getMessage());
    		resVo.setResult(true);
    		return resVo;
    	}
    	
    	List<StoreProductAttrValue> listAttrValue = storeProductAttrValueService.getList(queryMap);
    	if(listAttrValue!=null){
    		resVo.setCode(ProductResExceptionCode.BARCODE_EXTEND_ERROR.getCode());
    		resVo.setMessage(ProductResExceptionCode.BARCODE_EXTEND_ERROR.getMessage());
    		resVo.setResult(true);
    		return resVo;
    	}
    	resVo.setCode(ProductResExceptionCode.SUCCESS.getCode());
		resVo.setMessage(ProductResExceptionCode.SUCCESS.getMessage());
		resVo.setResult(result);
		return resVo;
	}

	
	
	/**
	 * 
	 * @param queryProductByPageStr
	 * @return
	 */
	@ApiOperation(value = "分页查询店铺下商品规格-SKU数据")
    @RequestMapping(value = "/queryProductSkuByPage",method = {RequestMethod. POST})
    public ResponseVo<PageInfo<StoreProductSkuDto>> queryProductSkuByPage(
    		@ApiParam(required = true, name = "queryProductSkuByPageStr", value = "入参为queryProductSkuByPageStr")
    		@RequestParam(value = "queryProductSkuByPageStr", required = true) String queryProductSkuByPageStr){
		
		log.info("msg1=开始分页查询店铺下商品规格-SKU数据,,method=queryProductSkuByPage,,reqVo={}",queryProductSkuByPageStr);
		StoreProductSkuQueryReqVo storeProductReqVo = JSONObject.parseObject(queryProductSkuByPageStr, StoreProductSkuQueryReqVo.class);
		
		String cateIdStr=storeProductReqVo.getCateId();
		Integer stroeId=storeProductReqVo.getStoreId();
		Integer pageIndex=storeProductReqVo.getPageIndex();
		Integer pageSize=storeProductReqVo.getPageSize();
		String storeNameOrBarCode=storeProductReqVo.getStoreNameOrBarCode();
		if(stroeId==null){
			ResponseVo<PageInfo<StoreProductSkuDto>> resVo=new ResponseVo<PageInfo<StoreProductSkuDto>>();
			resVo.setCode(ProductResExceptionCode.STOREID_NULL.getCode());
			resVo.setMessage(ProductResExceptionCode.STOREID_NULL.getMessage());
			return resVo;
		}
		
        List<Integer> cateIdLastList = new ArrayList<Integer>();
		List<Integer> productIdList=null;
		
		if(!StringUtils.isNullOrBlank(cateIdStr)){
			String cateIdArray[]=cateIdStr.split(",");
			for(String catestr:cateIdArray){
				Integer cateId=Integer.valueOf(catestr);
				cateIdLastList.add(cateId);
				List<StoreCategory> storeCategorylist = storeCategoryService.getStoreCategoryLastChild(cateId);
				
				for(StoreCategory storeCategory:storeCategorylist){
		        	cateIdLastList.add(storeCategory.getId());
		        }
			}
			productIdList=storeProductCateService.getProductIdListByCateIdList(cateIdLastList);
		}
		//根据顶层父类id查询所有叶子节点
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("storeId", stroeId);
		map.put("isShow", 1);
		map.put("isDel", 0);
		if(productIdList!=null && !productIdList.isEmpty()){
			map.put("productIdList", productIdList);
		}
		map.put("storeName", storeNameOrBarCode);
		PageInfo<StoreProductSkuDto> storeProductSkuList=storeProductService.getStoreProductSkuListByPage(map, pageIndex, pageSize);
		
		//循环设置想要的秒杀商品数量 start  暂时注释掉部署生产
		List<StoreProductSkuDto> listSkuDto=storeProductSkuList.getList();
		if(listSkuDto!=null && !listSkuDto.isEmpty()){
			for(StoreProductSkuDto dto:listSkuDto){
				Integer storeId=dto.getStoreId();
				Integer productAttrvalueId=dto.getAttrValueId();
				Integer wantNum=storeUserSeckillCollectService.countWantTotal(storeId, productAttrvalueId);
				if(wantNum==null){
					dto.setWantNum(0);
				}else{
					dto.setWantNum(wantNum);
				}
			}
		}
		//循环设置想要的秒杀商品数量 end
		ResponseVo<PageInfo<StoreProductSkuDto>> resVo=new ResponseVo<PageInfo<StoreProductSkuDto>>();
		resVo.setResult(storeProductSkuList);
		log.info("msg1=结束分页查询店铺下商品规格-SKU数据,,method=queryProductSkuByPage");
		
    	return resVo;
    }
	
	
	
	/**
	 * 
	 * @param queryProductByPageStr
	 * @return
	 */
	@ApiOperation(value = "门店收银扫码添加商品-查询店铺下商品规格SKU数据")
    @RequestMapping(value = "/queryProductSkuByBarCode",method = {RequestMethod. POST ,RequestMethod. GET })
    public ResponseVo<List<StoreProductSkuDto>> queryProductSkuByBarCode(
    		@ApiParam(required = true, name = "barCoce", value = "入参为barCoce")
    		@RequestParam(value = "barCoce", required = true) String barCoce,
    		@ApiParam(required = true, name = "stroeId", value = "入参为stroeId")
    		@RequestParam(value = "stroeId", required = true) Integer stroeId){
		
		log.info("msg1=开始门店收银扫码添加商品-查询店铺下商品规格SKU数据,,method=barCoce,,barCoce={}",barCoce);
		
		if(StringUtils.isBlank(barCoce)){
			ResponseVo<List<StoreProductSkuDto>> resVo=new ResponseVo<List<StoreProductSkuDto>>();
			resVo.setCode(ProductResExceptionCode.BARCODE_NULL.getCode());
			resVo.setMessage(ProductResExceptionCode.BARCODE_NULL.getMessage());
			return resVo;
		}
		
		if(stroeId==null){
			ResponseVo<List<StoreProductSkuDto>> resVo=new ResponseVo<List<StoreProductSkuDto>>();
			resVo.setCode(ProductResExceptionCode.STOREID_NULL.getCode());
			resVo.setMessage(ProductResExceptionCode.STOREID_NULL.getMessage());
			return resVo;
		}
		
		
		//根据顶层父类id查询所有叶子节点
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("storeId", stroeId);
		map.put("attrValueBarCode",barCoce);
		
		List<StoreProductSkuDto> storeProductSkuList=storeProductService.getStoreProductSkuByBarCode(map);
		
		ResponseVo<List<StoreProductSkuDto>> resVo=new ResponseVo<List<StoreProductSkuDto>>();
		resVo.setResult(storeProductSkuList);
		log.info("msg1=结束门店收银扫码添加商品-查询店铺下商品规格SKU数据,,method=queryProductSkuByBarCode");
		
    	return resVo;
    }
	
	
	
	
	@ApiOperation(value = "分享商品信息")
    @RequestMapping(value = "/queryShareStoreProductInfo", method = RequestMethod.POST)
    public ResponseVo<ShareStoreProductInfoVo> queryShareStoreProductInfo(
    		@ApiParam(required = true, name = "id", value = "商品id")
    		@RequestParam(value = "id", required = true) Integer id,
    		@ApiParam(required = true, name = "seckillId", value = "秒杀活动seckillId")
    		@RequestParam(value = "seckillId", required = false) Integer seckillId){
		
		log.info("msg1=分享商品开始,,method=queryShareStoreProductInfo,,id={},,seckillId={}",id,seckillId);
		
		ResponseVo<ShareStoreProductInfoVo> resVo=new ResponseVo<ShareStoreProductInfoVo>();
		
		if(id==null){
			resVo.setCode(ProductResExceptionCode.ID_NULL.getCode());
			resVo.setMessage(ProductResExceptionCode.ID_NULL.getMessage());
			return resVo;
		}
		StoreProduct storeProduct=storeProductService.queryByPrimaryId(id);
		ShareStoreProductInfoVo shareStoreProductInfoVo=new ShareStoreProductInfoVo();
		
		String shareSeckillImgUrl=null;
		String productName=null;
		
		if(seckillId!=null){
			StoreSeckill storeSeckill=storeSeckillService.queryByPrimaryId(seckillId);
			if(storeSeckill!=null){
				productName=storeSeckill.getProductName();
				BigDecimal seckilPrice=storeSeckill.getSeckillPrice();
				Double seckilPriceDouble=seckilPrice.doubleValue();
				shareStoreProductInfoVo.setSeckillPrice(String.valueOf(seckilPriceDouble));
				shareSeckillImgUrl=storeSeckill.getShareImageUrl();
				shareStoreProductInfoVo.setStoreName(productName);
			}
		}
		
		
		if(storeProduct!=null){
			
			String qrCodeImage=storeProduct.getWechatappletQrcodeShareImage();
			//商品信息
			String image=storeProduct.getImage();
			BigDecimal price=storeProduct.getPrice();
			productName=storeProduct.getStoreName();
			Integer storeId=storeProduct.getStoreId();
			shareStoreProductInfoVo.setId(id);
			shareStoreProductInfoVo.setImage(image);
			shareStoreProductInfoVo.setPrice(String.valueOf(price.doubleValue()));
			shareStoreProductInfoVo.setStoreName(productName);
			
			//暂时写死 每次随机获取
			List<String> listUserAvatar=getRandomAvatarList();
			shareStoreProductInfoVo.setListUserAvatar(listUserAvatar);
			
			String lastMergImageUrl=null;
			
//			if(StringUtils.isNullOrBlank(qrCodeImage)){
				//第二种方式生成二维码  https://dev.bianwaixingqiu.com/goodsdetail?invite=true&id=&seckillId=
//				String qrCodeContent=shareProductUrl + "&"+shareProductId+id+"&"+shareSeckillId+seckillId; 
				//二维码里面设置内容
			    String qrCodeContent=null;
				//如果秒杀活动id不为空则拼接秒杀url
				if(seckillId!=null && seckillId>0){
//					if(StringUtils.isNullOrBlank(shareSeckillImgUrl)){
						qrCodeContent=shareProductUrl+"&id="+id+"&seckillId="+seckillId+"&type=seckill"+"&storeId="+storeId; 
						lastMergImageUrl = getImageUrl(image, qrCodeContent);
//					}else{
//						lastMergImageUrl=shareSeckillImgUrl;
//					}
					//更新秒杀url
					StoreSeckill updateStoreSeckill=new StoreSeckill();
					updateStoreSeckill.setId(seckillId);
					updateStoreSeckill.setShareImageUrl(lastMergImageUrl);
					storeSeckillService.update(updateStoreSeckill);
				}else{
					if(StringUtils.isNullOrBlank(qrCodeImage)){
						qrCodeContent=shareProductUrl+"&id="+id+"&storeId="+storeId;
						lastMergImageUrl = getImageUrl(image, qrCodeContent);
					}else{
						lastMergImageUrl=qrCodeImage;
					}
					//更新商品分享二维码
				     StoreProduct updateStoreProduct=new StoreProduct();
				     updateStoreProduct.setId(id);
				     updateStoreProduct.setWechatappletQrcodeShareImage(lastMergImageUrl);
				     updateStoreProduct.setShareTime(new Date());
				     storeProductService.update(updateStoreProduct);
				}
				
			shareStoreProductInfoVo.setProductUrl(lastMergImageUrl);
			logger.info("msg1=生成商品分享二维码,聚合图片路径,,method=queryShareStoreProductInfo,,lastMergImageUrl={}",lastMergImageUrl);
			
			//太阳二维码
			/*if(StringUtils.isNullOrBlank(qrCodeImage)){
				//获取门店小程序二维码
				String token=null;
				try {
					token = WxQrCodeUtil.postToken(WXCommonConstants.appId, WXCommonConstants.AppSecret);
				} catch (Exception e) {
					e.printStackTrace();
					resVo.setCode(ProductResExceptionCode.GET_TOKEN_ERROR.getCode());
					resVo.setMessage(ProductResExceptionCode.GET_TOKEN_ERROR.getMessage());
					return resVo;
				}
				 String scene="id="+id;
				 //小程序地址要替换为商品路径 待陈勇提供
				 InputStream in=WxQrCodeUtil.generateQrCode("pages/goods/goodsDetail/goodsDetail", scene, token);
				 
				 BufferedImage qrCodeBufferImage = null;
				 try {
					qrCodeBufferImage = ImageIO.read(in);
				} catch (IOException e) {
					e.printStackTrace();
					resVo.setCode(ProductResExceptionCode.GET_QRCODE_IMAGE_ERROR.getCode());
					resVo.setMessage(ProductResExceptionCode.GET_QRCODE_IMAGE_ERROR.getMessage());
					return resVo;
				}
				
				 String logoUrl=image;
			     BufferedImage newsunImage=ImageMergeUtil.replaceSunCodeLogo (qrCodeBufferImage,logoUrl);
				
			     String lastMergImageUrl=null;
			     //bufferedimage 转换成 inputstream
		        ByteArrayOutputStream bs = new ByteArrayOutputStream();
				try {
					 ImageOutputStream imOut = ImageIO.createImageOutputStream(bs);
					 ImageIO.write(newsunImage, "jpg", imOut);
					 InputStream inputStream = new ByteArrayInputStream(bs.toByteArray());
					 lastMergImageUrl=StorageUploadUtil.ossUploadFile(inputStream, "jpg");
					 
				} catch (Exception e) {
					logger.error("msg1=生成商品分享二维码失败,,method=queryShareStoreProductInfo");;
					e.printStackTrace();
					resVo.setCode(ProductResExceptionCode.PRODUCT_QRCODE_IMAGE_ERROR.getCode());
					resVo.setMessage(ProductResExceptionCode.PRODUCT_QRCODE_IMAGE_ERROR.getMessage());
					return resVo;
					
				}
		        logger.info("msg1=生成商品分享二维码,聚合图片路径,,method=queryShareStoreProductInfo,,lastMergImageUrl={}",lastMergImageUrl);
			     //设置商品二维码图片链接
			     shareStoreProductInfoVo.setProductUrl(lastMergImageUrl);
			     //更新商品分享二维码
			     StoreProduct updateStoreProduct=new StoreProduct();
			     updateStoreProduct.setId(id);
			     updateStoreProduct.setWechatappletQrcodeShareImage(lastMergImageUrl);
			     storeProductService.update(updateStoreProduct);
			}else{
				shareStoreProductInfoVo.setProductUrl(qrCodeImage);
			}*/
		}
		resVo.setResult(shareStoreProductInfoVo);
		return resVo;
    }



	private String getImageUrl(String image,String qrCodeContent) {
		//bufferedimage 转换成 inputstream
		String lastMergImageUrl=null;
		ByteArrayOutputStream bs = new ByteArrayOutputStream();
		try {
			BufferedImage newsunImage=QRCodeUtil.encodenew(qrCodeContent,image, true);
			ImageOutputStream imOut = ImageIO.createImageOutputStream(bs);
			ImageIO.write(newsunImage, "jpg", imOut);
			InputStream inputStream = new ByteArrayInputStream(bs.toByteArray());
			lastMergImageUrl=StorageUploadUtil.ossUploadFile(inputStream, "jpg");
		} catch (Exception e) {
			logger.info("msg1=生成图片链接异常,,method=getImageUrl",e);
			e.printStackTrace();
			throw new ProductResException(ProductResExceptionCode.GET_QRCODE_IMAGE_ERROR.getCode(),ProductResExceptionCode.GET_QRCODE_IMAGE_ERROR.getMessage());
		}
		return lastMergImageUrl;
	}



	private List<String> getRandomAvatarList() {
		List<String> listUserAvatar=new ArrayList<String>();
		listUserAvatar.add("https://thirdwx.qlogo.cn/mmhead/th072XaRV0dSwM0U8A561gZYCKiaY9fDI8V9DpI2Fvo0/132");
		listUserAvatar.add("https://thirdwx.qlogo.cn/mmhead/KWoYp47Wh0SAib2BXsgkFMgAKA3GUmMseCicSZSDGGxpE/132");
		listUserAvatar.add("https://thirdwx.qlogo.cn/mmhead/g9lhfAVmJQa1jE6FB6OXxP9g54MFibIFszaEfQzwSMlE/132");
		listUserAvatar.add("https://thirdwx.qlogo.cn/mmhead/DwjRuMYhJvaiasFr5G5FuxGhdq4SvviaRHeqEZKn1xDaY/132");
		listUserAvatar.add("https://thirdwx.qlogo.cn/mmhead/4RUGvRCNErBLSKY9HTic9oibzsh3r11miarXMKbeFWRBJ4/132");
		listUserAvatar.add("https://thirdwx.qlogo.cn/mmhead/oqUI5uibuYKicXSxicbg2xDe4Oxic1G2lJn31M8ejVY59yA/132");
		listUserAvatar.add("https://thirdwx.qlogo.cn/mmhead/5tCU4Bs3tY11Hn7G6YlfGnj0yOuxtNjpqungWFJSabA/132");
		listUserAvatar.add("https://thirdwx.qlogo.cn/mmhead/EkFRBKqwyR8tEojEv7DpNEp8aWxWPTVLylnd2k5PPFE/132");
		listUserAvatar.add("https://thirdwx.qlogo.cn/mmhead/1ZU9qTESZetEBbKnmJNTdhrnNufnUMzYHD5D11yGFrs/132");
		listUserAvatar.add("https://thirdwx.qlogo.cn/mmhead/nfroaKibpibjyujfDTkebVpbID15hql3iag2ibko32IZNlE/132");
		listUserAvatar.add("https://thirdwx.qlogo.cn/mmhead/xnUkTubMQuPZZSsmkRoaqOUwRo6fV0VpT8MeHfQqX1s/132");
		listUserAvatar.add("https://thirdwx.qlogo.cn/mmhead/Aia5pFwBiaGeatR2wUYBqN6OqpXpXQV7j9BDY6wq8vODE/132");
		listUserAvatar.add("https://thirdwx.qlogo.cn/mmhead/F1rwqfhqPQy4LczuG7RjY73MvLOvibnTic8yht1fFzmD8/132");
		Collections.shuffle(listUserAvatar);
		return listUserAvatar;
	}
	
	
	@ApiOperation(value = "修改商品标签")
	@RequestMapping(value = "/updateProductLabel", method = RequestMethod.POST)
    public ResponseVo<Boolean> updateProductLabel(
	    @ApiParam(required = true, name = "id", value = "商品id")
		@RequestParam(value = "id", required = true) Integer id,		
		@ApiParam(required = true, name = "productLabel", value = "商品标签名称")
		@RequestParam(value = "productLabel", required = true) String productLabel){
		
		log.info("msg1=修改商品标签开始,,method=updateProductLabel,,id={},,productLabel={}",id,productLabel);
		
		ResponseVo<Boolean> resVo=new ResponseVo<Boolean>();
		
		StoreProduct storeProduct=storeProductService.queryByPrimaryId(id);
		if(storeProduct==null){
			resVo.setCode(ProductResExceptionCode.PRODUCT_NOT_EXTEND_ERROR.getCode());
			resVo.setMessage(ProductResExceptionCode.PRODUCT_NOT_EXTEND_ERROR.getMessage());
			resVo.setResult(false);
			return resVo;
		}
		
		if(StringUtils.isNullOrBlank(productLabel)){
			resVo.setCode(ProductResExceptionCode.PRODUCT_LABEL_NULL.getCode());
			resVo.setMessage(ProductResExceptionCode.PRODUCT_LABEL_NULL.getMessage());
			resVo.setResult(false);
			return resVo;
		}
		
		String productLabelStr=storeProduct.getProductLabel();
		
		
		StoreProduct updateStoreProduct=new StoreProduct();
		updateStoreProduct.setId(id);
		
		if(!StringUtils.isNullOrBlank(productLabelStr)){
			List<String> productLabelList = Arrays.asList(productLabelStr.split(","));
			List<String> arrList = new ArrayList(productLabelList);
			if(!arrList.contains(productLabel)){
				arrList.add(productLabel);
			}
			Collections.sort(arrList);
			String labelStr = Joiner.on(",").join(arrList);
			updateStoreProduct.setProductLabel(labelStr);
		}else{
			updateStoreProduct.setProductLabel(productLabel);
		}
		
		storeProductService.update(updateStoreProduct);
		
		resVo.setCode(ProductResExceptionCode.SUCCESS.getCode());
		resVo.setMessage(ProductResExceptionCode.SUCCESS.getMessage());
		resVo.setResult(true);
		log.info("msg1=修改商品标签结束,,method=updateProductLabel");
    	return resVo;
    }
	
	
	
}